package com.iweb.shopping.servlet;

import com.alibaba.fastjson.JSONObject;
import com.iweb.shopping.model.Address_sh;
import com.iweb.shopping.model.Cart;
import com.iweb.shopping.model.Goods;
import com.iweb.shopping.model.Goodsdetail;
import com.iweb.shopping.service.ShoppingService;
import com.iweb.shopping.service.ShoppingServiceImpl;
import com.iweb.user.model.User;
import com.iweb.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "*.shopping")
public class ShoppingServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        String path = req.getServletPath();
        switch (path){
            case"/toList.shopping":
                req.getRequestDispatcher("/shopping/shopping_selectByPage.html").forward(req,resp);
                break;
            case"/selectByPage.shopping":
                selectByPage(req,resp);
                break;
            case"/toQuery.shopping":
                toQuery(req,resp);
                break;
            case"/queryByGoodsId.shopping":
                queryByGoodsId(req,resp);
                break;
            case"/queryAddress.shopping":
                queryAddress(req,resp);
                break;
            case"/queryAddressById.shopping":
                queryAddressById(req,resp);
                break;
            case"/deleteById.shopping":
                deleteById(req,resp);
                break;
            case"/saveOne.shopping":
                saveOne(req,resp);
                break;
            default:
                break;
        }
    }
    ShoppingService shoppingService = new ShoppingServiceImpl();
    public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session.getAttribute("user") == null){
            resp.getWriter().print("");
            return;
        }
        String pageNo = req.getParameter("pageNo") == null?"1":req.getParameter("pageNo");
        String likeValue = req.getParameter("likeValue") == null?"":req.getParameter("likeValue");
        PageModel pageModel = new PageModel();
        pageModel.setPageNo(Integer.valueOf(pageNo));
        pageModel.setLikeValue(likeValue);

        Object userObj = session.getAttribute("user");
        User user = (User) userObj;
        Integer userid = user.getUserid();

        List<Goods> goods = shoppingService.selectByPage(pageModel,userid);
        pageModel.setRecordList(goods);
        String jsonStr = JSONObject.toJSONString(pageModel);
        resp.getWriter().print(jsonStr);
    }

    public void toQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodsid = req.getParameter("goodsid");
        resp.sendRedirect(req.getContextPath()+"/shopping/goods_detail.html?goodsid="+goodsid);
    }
    /*
    查询一条记录
     */
    public void queryByGoodsId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodsid = req.getParameter("goodsid");
        Goodsdetail goodsdetail = shoppingService.selectById(Integer.valueOf(goodsid));
        String jsonStr = JSONObject.toJSONString(goodsdetail);
        resp.getWriter().print(jsonStr);
    }

    public void queryAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session.getAttribute("user") == null){
            resp.getWriter().print("");
            return;
        }
        Object userObj = session.getAttribute("user");
        User user = (User) userObj;
        Integer userid = user.getUserid();

        List<Address_sh> address_shs = shoppingService.queryAddress(userid);
        System.out.println(address_shs.size());
        String jsonStr = JSONObject.toJSONString(address_shs);
        resp.getWriter().print(jsonStr);
    }
    public void queryAddressById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String addressid = req.getParameter("addressid");
        Address_sh address_sh = shoppingService.queryAddressById(Integer.valueOf(addressid));
//        System.out.println(address_sh.getAddressid());
        String jsonStr = JSONObject.toJSONString(address_sh);
        resp.getWriter().print(jsonStr);
    }
    public void deleteById (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cartid = req.getParameter("cartid");
        shoppingService.deleteOne(Integer.valueOf(cartid));
        resp.sendRedirect(req.getContextPath()+"/toList.shopping?pageNo=1&likeValue=");
    }
    public void saveOne (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object userObj = session.getAttribute("user");
        User user = (User) userObj;
        Integer buyerid= user.getUserid();
        String goodsid = req.getParameter("goodsid");
        Cart cart = new Cart(null,Integer.valueOf(goodsid),buyerid,null);
        System.out.println(cart.toString());
        shoppingService.saveOne(cart);
        resp.sendRedirect(req.getContextPath()+"/toList.shopping?pageNo=1&likeValue=");
    }


}
